﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using GUESTSMANAGEMENT.Controller;
using System.Collections.Generic;
using Coolite.Ext.Web;

namespace GUESTSMANAGEMENT.View
{
    public partial class WebUserControl1 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
                    
        }

        protected void Load(object sender, AjaxEventArgs e)
        {
            using (guestsmanagementEntities context = new guestsmanagementEntities())
            {
                try
                {
                    var selectListGuests = from guestlists in context.gmlistguests.Include("gmuser.nmuser")
                                           from gevent in context.gmevent.Include("gmevent.nmevent")
                                           from user in context.gmuser.Include("gmuser.nmuser")
                                           from guests in context.gmguest.Include("gmguests.nmguests")
                                           where guestlists.GMUSER_CDUSER == user.CDUSER && guestlists.GMEVENT_CDEVENT == gevent.CDEVENT
                                           select new { guestlists.CDLISTGUESTS, guests.NMGUEST, guests.NMPHONE, guests.NMMAIL, user.NMUSER, gevent.NMEVENT, gevent.DTEVENT };
                    var lista = selectListGuests.ToList();

                    if (!Ext.IsAjaxRequest)
                    {
                        this.listStore.DataSource = lista;
                        this.listStore.DataBind();
                    }
                }
                catch
                {
                    Ext.Msg.Alert("Alerta!", "Ocorreu um erro durante a transação! Tente novamente!").Show();
                }
            }
        
        }

        public Store GridStore
        {
            get;
            set;
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            try
            {
                this.GMListsGuests.GridStore = this.listStore;
                this.GridStore = this.listStore;
            }
            catch
            {
                Ext.Msg.Alert("Alerta!", "Ocorreu um erro durante a transação! Tente novamente!").Show();
            }
        }

        public void SetListGuests(object sender, AjaxEventArgs e)
        {
            try
            {
                gmevent evento = Controller.Event.getByEvent(int.Parse(this.txt_idCdEvent.Text));
                Session["eventId"] = (int)evento.CDEVENT;

                this.ScriptManagerProxy.AddScript("newListGuests();");
            }
            catch
            {
                Ext.Msg.Alert("Alerta!", "Ocorreu um erro durante a transação! Tente novamente!").Show();
            }
        }

        protected void Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            try
            {
                DateTime date = this.eventDate.SelectedDate;
                gmevent evento = Controller.Event.getByEventDate(date);
                int cdUser = Convert.ToInt32(Session["userCd"]);
                using (guestsmanagementEntities context = new guestsmanagementEntities())
                {
                    if ((evento != null) && (cdUser != null))
                    {
                        List<gmlistguests> listGuests = Controller.GuestsList.getGridListGuests(cdUser, (int)evento.CDEVENT);
                        this.listStore.DataSource = listGuests;
                        this.listStore.DataBind();

                        gmeventuser eventUser = (from eventuser in context.gmeventuser
                                                 where eventuser.gmuser.CDUSER == cdUser && eventuser.gmevent.CDEVENT == evento.CDEVENT
                                                 select eventuser).FirstOrDefault();
                        this.btnNewListsGuests.Enabled = false;
                        if (eventUser != null)
                        {
                            this.btnNewListsGuests.Enabled = true;
                        }

                        List<gmlistguests> listGuestsQTBF = Controller.GuestsList.getByListsGuestsInvites((int)evento.CDEVENT, cdUser, 1);
                        List<gmlistguests> listGuestsQTBM = Controller.GuestsList.getByListsGuestsInvites((int)evento.CDEVENT, cdUser, 2);
                        List<gmlistguests> listGuestsQTFF = Controller.GuestsList.getByListsGuestsInvites((int)evento.CDEVENT, cdUser, 3);
                        List<gmlistguests> listGuestsQTFM = Controller.GuestsList.getByListsGuestsInvites((int)evento.CDEVENT, cdUser, 4);

                        int qtbf, qtbm, qtff, qtfm;

                        qtbf = listGuestsQTBF.Count;
                        qtbm = listGuestsQTBM.Count;
                        qtff = listGuestsQTFF.Count;
                        qtfm = listGuestsQTFM.Count;

                        this.Nqtbf.Text = ((int)eventUser.QTBF - qtbf).ToString();
                        this.Nqtbm.Text = ((int)eventUser.QTBM - qtbm).ToString();
                        this.Nqtff.Text = ((int)eventUser.QTFF - qtff).ToString();
                        this.Nqtfm.Text = ((int)eventUser.QTFM - qtfm).ToString();
                        this.TimeList.Text = evento.TMLIST.ToString();
                    }
                }
            }
            catch
            {
                Ext.Msg.Alert("Alerta!", "Ocorreu um erro durante a transação! Tente novamente!").Show();
            }

        }

        protected void EnventDateSelected(object sender, AjaxEventArgs e)
        {
            try
            {
                DateTime date = this.eventDate.SelectedDate;
                gmevent evento = Controller.Event.getByEventDate(date);
                using (guestsmanagementEntities context = new guestsmanagementEntities())
                {
                    if (evento != null)
                    {
                        int cdUser = Convert.ToInt32(Session["userCd"]);

                        gmeventuser eventUser = (from eventuser in context.gmeventuser
                                                 where eventuser.gmuser.CDUSER == cdUser && eventuser.gmevent.CDEVENT == evento.CDEVENT
                                                 select eventuser).FirstOrDefault();
                        this.btnNewListsGuests.Enabled = false;
                        if (eventUser != null)
                        {
                            this.btnNewListsGuests.Enabled = true;

                            List<gmlistguests> listGuests = Controller.GuestsList.getGridListGuests(cdUser, (int)evento.CDEVENT);

                            List<gmlistguests> listGuestsQTBF = Controller.GuestsList.getByListsGuestsInvites((int)evento.CDEVENT, cdUser, 1);
                            List<gmlistguests> listGuestsQTBM = Controller.GuestsList.getByListsGuestsInvites((int)evento.CDEVENT, cdUser, 2);
                            List<gmlistguests> listGuestsQTFF = Controller.GuestsList.getByListsGuestsInvites((int)evento.CDEVENT, cdUser, 3);
                            List<gmlistguests> listGuestsQTFM = Controller.GuestsList.getByListsGuestsInvites((int)evento.CDEVENT, cdUser, 4);

                            int qtbf, qtbm, qtff, qtfm;

                            qtbf = listGuestsQTBF.Count;
                            qtbm = listGuestsQTBM.Count;
                            qtff = listGuestsQTFF.Count;
                            qtfm = listGuestsQTFM.Count;

                            this.listStore.DataSource = listGuests;
                            this.listStore.DataBind();

                            this.Nqtbf.Text = ((int)eventUser.QTBF - qtbf).ToString();
                            this.Nqtbm.Text = ((int)eventUser.QTBM - qtbm).ToString();
                            this.Nqtff.Text = ((int)eventUser.QTFF - qtff).ToString();
                            this.Nqtfm.Text = ((int)eventUser.QTFM - qtfm).ToString();
                            this.TimeList.Text = evento.TMLIST.ToString();
                            this.eventName.Text = evento.NMEVENT;
                            this.GridPanel1.Enabled = true;
                            this.txt_idCdEvent.Text = evento.CDEVENT.ToString();
                            this.txt_idCdUser.Text = cdUser.ToString();
                        }
                        else
                        {
                            Ext.Msg.Alert("Alerta!", "Não há eventos cadastros nesta data!").Show();
                        }
                    }
                    else
                    {
                        Ext.Msg.Alert("Alerta!", "Não há eventos cadastros nesta data!").Show();
                        this.GridPanel1.Reload();
                    }
                }
            }
            catch
            {
                Ext.Msg.Alert("Alerta!", "Ocorreu um erro durante a transação! Tente novamente!").Show();
            }
        }

        protected void ConfirmDeleteListGuest(object sender, AjaxEventArgs e)
        {
            try
            {
                string selectListGuest = this.txt_idGuestsList.Text.Trim();
                gmevent evento = Controller.Event.getByEvent(int.Parse(this.txt_idCdEvent.Text));
                
                string teste = evento.DTEVENT.ToString().Replace("00:00:00", evento.TMLIST.ToString());

                DateTime date = Convert.ToDateTime(teste);
                if (!((DateTime.Compare(DateTime.Now, date)) >= 0))
                {

                    gmlistguests gmListGuest = Controller.GuestsList.getByListsGuests(int.Parse(selectListGuest));

                    gmuser promoter = Controller.User.getByUser((int)gmListGuest.GMUSER_CDUSER); 

                    GMListsGuests.EnviarEmail(gmListGuest.NMMAILGUEST, gmListGuest.NMGUEST, promoter, evento, (int)gmListGuest.FGTYPEINVITE,2);
                    
                    Controller.GuestsList.deleteListGuest(gmListGuest);
                    this.GridStore.AddScript(string.Format("{0}.reload();", this.GridStore.ClientID));
                    Ext.Msg.Alert("Alerta!", "Convidado <b>" + gmListGuest.NMGUEST + "</b> <br>Removido com sucesso da <br>" + evento.NMEVENT).Show();

                    int cdUser = Convert.ToInt32(Session["userCd"]);

                    List<gmlistguests> listGuests = Controller.GuestsList.getGridListGuests(cdUser, (int)evento.CDEVENT);
                    this.listStore.DataSource = listGuests;
                    this.listStore.DataBind();

                    List<gmlistguests> listGuestsQTBF = Controller.GuestsList.getByListsGuestsInvites((int)evento.CDEVENT, cdUser, 1);
                    List<gmlistguests> listGuestsQTBM = Controller.GuestsList.getByListsGuestsInvites((int)evento.CDEVENT, cdUser, 2);
                    List<gmlistguests> listGuestsQTFF = Controller.GuestsList.getByListsGuestsInvites((int)evento.CDEVENT, cdUser, 3);
                    List<gmlistguests> listGuestsQTFM = Controller.GuestsList.getByListsGuestsInvites((int)evento.CDEVENT, cdUser, 4);

                    int qtbf, qtbm, qtff, qtfm;

                    qtbf = listGuestsQTBF.Count;
                    qtbm = listGuestsQTBM.Count;
                    qtff = listGuestsQTFF.Count;
                    qtfm = listGuestsQTFM.Count;

                    this.Nqtbf.Text = ((int)evento.QTBF - qtbf).ToString();
                    this.Nqtbm.Text = ((int)evento.QTBM - qtbm).ToString();
                    this.Nqtff.Text = ((int)evento.QTFF - qtff).ToString();
                    this.Nqtfm.Text = ((int)evento.QTFM - qtfm).ToString();
                    this.TimeList.Text = evento.TMLIST.ToString();
                }
                else
                {
                    Ext.Msg.Alert("Alerta!", "Tempo limite para alteração de convidados esgotou!").Show();
                }
            }
            catch
            {
                Ext.Msg.Alert("Alerta!", "Ocorreu um erro durante a transação! Tente novamente!").Show();            
            }

        }
    }
}